package leetcode.code977.SquaresofaSortedArray;

/**
 * 写法很简练 <br>
 * 学到了
 *
 * @author Max
 * @date 2021年8月13日
 * @description
 */
public class Solution3 implements ISquaresofaSortedArray {

	@Override
	public int[] sortedSquares(int[] nums) {
		int n = nums.length;
		int[] ans = new int[n];
		for (int i = 0, j = n - 1, pos = n - 1; i <= j;) {
			if (nums[i] * nums[i] > nums[j] * nums[j]) {
				ans[pos] = nums[i] * nums[i];
				++i;
			} else {
				ans[pos] = nums[j] * nums[j];
				--j;
			}
			--pos;
		}
		return ans;
	}

	public static void main(String[] args) {
		Solution3 so = new Solution3();
		so.myPrint1();
		so.myPrint2();
		so.myPrint3();
		so.myPrint4();
	}

//	输入：nums = [-4,-1,0,3,10]
//  输出：[0,1,9,16,100]
//	解释：平方后，数组变为 [16,1,0,9,100]
//	排序后，数组变为 [0,1,9,16,100]

	public void myPrint1() {
		int[] nums = { -4, -1, 0, 3, 10 };
		this.debug(nums);
	}

//	输入：nums = [-7,-3,2,3,11]
//	输出：[4,9,9,49,121]

	public void myPrint2() {
		int[] nums = { -7, -3, 2, 3, 11 };
		this.debug(nums);
	}

//	输入：nums = [-1]
//	输出：[1]

	public void myPrint3() {
		int[] nums = { -1 };
		this.debug(nums);
	}

//	[-5,-3,-2,-1]

	public void myPrint4() {
		int[] nums = { -5, -3, -2, -1 };
		this.debug(nums);
	}

	private void debug(int[] nums) {
		ISquaresofaSortedArray solution = new Solution3();
		int[] result = solution.sortedSquares(nums);
		for (int i : result) {
			System.out.print(i + ",");
		}
		System.out.println(" ");
	}

}
